package com.cskaoyan.service;

import com.cskaoyan.bean.Issue;
import com.cskaoyan.bean.IssueExample;
import com.cskaoyan.bean.OrderExample;
import com.cskaoyan.mapper.IssueMapper;
import com.cskaoyan.vo.IssueVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: libugu@cskaoyan.com
 * @date 2021/4/9 - 12:24
 */
@Service
public class IssueServiceImpl implements IssueService {

    @Autowired
    IssueMapper issueMapper;

    @Override
    public IssueVo query(Integer page, Integer limit, String sort, String order,String question) {

        PageHelper.startPage(page, limit);

        IssueExample issueExample = new IssueExample();
        issueExample.setOrderByClause(sort + " " + order);
        IssueExample.Criteria criteria = issueExample.createCriteria();
        if (question != null && !"".equals(question)) {

            criteria.andQuestionLike("%" + question + "%");
        }

        List<Issue> issues = issueMapper.selectByExample(issueExample);

        PageInfo<Issue> issuePageInfo = new PageInfo<>();
        long total = issuePageInfo.getTotal();

        IssueVo issueVo = new IssueVo();
        issueVo.setItems(issues);
        issueVo.setTotal(total);

        return issueVo;
    }

    @Override
    public Issue solve(Issue issue) {


        issue.setUpdateTime(new Date());
        issue.setAddTime(new Date());
        issue.setQuestion(issue.getQuestion());
        issue.setAnswer(issue.getAnswer());
        issue.setId(issue.getId());
        int insert = issueMapper.insertIssue(issue);

        return issue;
    }

    @Override
    public void delete(Issue issue) {
        IssueExample issueExample = new IssueExample();
        Integer id = issue.getId();
        issueMapper.deleteByPrimaryKey(id);

    }

    @Override
    public Issue update(Issue issue) {

        IssueExample issueExample = new IssueExample();


        issue.setUpdateTime(new Date());
        issue.setAnswer(issue.getAnswer());
        issue.setQuestion(issue.getQuestion());
        Integer id = issue.getId();
        issueMapper.updateByPrimaryKey(issue);
        return issue;
    }
}
